package  com.sat.common.widget

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import  com.sat.common.R
import  com.sat.common.utils.ScreenUtil

/**
 * @Description: 提示窗口
 */
class TipDialog : Dialog {
    private var tvTitle: TextView? = null
    private var tvContent: TextView? = null
    private var tvCancel: TextView? = null
    private var tvSure: TextView? = null
    private var mListener: View.OnClickListener?
    private var mContentView: View? = null
    var isDismiss = true // 点击确认按钮后是否隐藏；

    constructor(
        context: Context,
        listener: View.OnClickListener?
    ) : super(context, R.style.Alert_Dialog_Style) {
        mListener = listener
        init(context)
        isDismiss = true
    }

    // 自定义布局
    constructor(
        context: Context,
        id: Int,
        listener: View.OnClickListener?
    ) : super(context, R.style.Alert_Dialog_Style) {
        mContentView = View.inflate(context, id, null)
        mListener = listener
        init(context)
    }

    constructor(
        context: Context,
        contentView: View?,
        listener: View.OnClickListener?
    ) : super(context, R.style.Alert_Dialog_Style) {
        this.mContentView = contentView
        mListener = listener
        init(context)
    }

    private fun init(context: Context) {
        setCanceledOnTouchOutside(false)
        if (mContentView == null) {
            // 未指定布局使用默认布局
            mContentView = View.inflate(context, R.layout.layout_tip_dialog, null)
        }
        setContentView(mContentView!!)
        init(mContentView)
    }

    private fun init(view: View?) {
        tvContent = view!!.findViewById(R.id.tvContent)
        tvTitle = view.findViewById(R.id.tvTitle)
        tvCancel = view.findViewById(R.id.tvCancel)
        tvSure = view.findViewById(R.id.tvSure)
        if (tvSure != null) {
            tvSure!!.setOnClickListener { arg0 ->
                if (mListener != null) {
                    mListener!!.onClick(arg0)
                }
                if (isDismiss) dismiss()
            }
        }
        if (tvCancel != null) {
            tvCancel!!.setOnClickListener { // TODO Auto-generated method stub
                dismiss()
            }
        }
        val window = window
        val params = window!!.attributes
        params.gravity = Gravity.CENTER
        params.alpha = 1.0f
        params.width = ((ScreenUtil.getScreenWidth(context) * 0.86).toInt())
        params.height = WindowManager.LayoutParams.WRAP_CONTENT // ScreenUtil.dip2px(150);//120改为150
        params.dimAmount = 0.4f
        window.attributes = params
    }

    fun setTitle(title: String?) {
        tvTitle!!.text = title
    }

    fun setContent(content: String?) {
        tvContent!!.text = content
    }

    fun setContent(resId: Int) {
        tvContent!!.setText(resId)
    }

    fun setCancelStr(cancel: String?) {
        tvCancel!!.text = cancel
    }

    fun setSureStr(sure: String?) {
        tvSure!!.text = sure
    }

    fun setCancelVisible(visible: Int) {
        tvCancel!!.visibility = visible
    }

    fun setSureVisible(visible: Int) {
        tvSure!!.visibility = visible
    }

    fun setCancelOnClickListener(listener: View.OnClickListener) {
        tvCancel!!.setOnClickListener { arg0 ->
            listener.onClick(arg0)
            dismiss()
        }
    }

    fun setTag(obj: Any?) {
        tvSure!!.tag = obj
    }

    fun getViewById(id: Int): View? {
        return mContentView?.findViewById<View>(id)
    }


}
